package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = roleService.findPage(queryPageBean);
        return page;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Role role,Integer[] permissionIds,Integer[] menuIds){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true,"添加角色成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加角色失败");
        }
    }
    @RequestMapping("/delete")
    public Result deleteByRoleId(Integer id){
        try {
            roleService.deleteByRoleId(id);
            return new Result(true, "角色删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "角色删除失败");
        }

    }
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu> allMenu = roleService.findAllMenu();
            return new Result(true,"查询成功",allMenu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true,"查询成功",role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");
        }
    }
    @RequestMapping("/findCheckMenuIdsByRoleId")
    public Result findCheckMenuIdsByRoleId(Integer id){
        try {
            List<Integer> checkMenuIdsByRoleId = roleService.findCheckMenuIdsByRoleId(id);
            return new Result(true,"",checkMenuIdsByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
    @RequestMapping("/findCheckPermissionIdsByRoleId")
    public Result findCheckPermissionIdsByRoleId(Integer id){
        try {
            List<Integer> checkPermissionIdsByRoleId = roleService.findCheckPermissionIdsByRoleId(id);
            return new Result(true,"查询成功",checkPermissionIdsByRoleId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"失败");
        }
    }
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role,Integer[] menuIds,Integer[] permissionIds){
        try {
            roleService.edit(role,menuIds,permissionIds);
            return new Result(true,"编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑失败");
        }
    }

    @RequestMapping("/findAllZP")
    public Result findAllZP(){
//        try {
        List<Role> list=roleService.findAllZP();
        return new Result(true,"查询所有角色成功",list);
       /* } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询所有角色失败");
        }*/
    }
}
